package com.patelski.magic.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.log.Log;

import com.patelski.magic.exception.MtgException;
import com.patelski.magic.model.Card;
import com.patelski.magic.model.Color;
import com.patelski.magic.model.Edition;
import com.patelski.magic.service.CardList;

@Stateful
@Name("collectorNumbers")
public class SetCollectorNumbers implements SetCollectorNumbersInterface {

	@PersistenceContext(type = PersistenceContextType.EXTENDED)
	protected EntityManager em;

	@Logger
	protected Log log;

	@In(create=true) 
	private CardList cardList;

	/* (non-Javadoc)
	 * @see com.patelski.magic.util.SetCollectorNumbersInterface#destroy()
	 */
	@Remove
	public void destroy() {
	}

	/* (non-Javadoc)
	 * @see com.patelski.magic.util.SetCollectorNumbersInterface#run()
	 */
	public void run() {
		
//		setAllCollectorNumbers("A");
//		setAllCollectorNumbers("B");
//		setAllCollectorNumbers("U");
//		setAllCollectorNumbers("R");
//		setAllCollectorNumbers("4E");
//		setAllCollectorNumbers("5E");
//		setAllCollectorNumbers("6E");
//		setAllCollectorNumbers("7E");
//		setAllCollectorNumbers("8E");
//		setAllCollectorNumbers("9E");
//		setAllCollectorNumbers("10E");
//
//
//		setAllCollectorNumbers("AN");
//		setAllCollectorNumbers("AQ");
//		setAllCollectorNumbers("LG");
//		setAllCollectorNumbers("DK");
//		setAllCollectorNumbers("FE");
//		setAllCollectorNumbers("HL");
//
//		setAllCollectorNumbers("IA");
//		setAllCollectorNumbers("AL");
//		setAllCollectorNumbers("CS");

//		setAllCollectorNumbers("MI");
//		setAllCollectorNumbers("VI");
//		setAllCollectorNumbers("WL");
//
//		setAllCollectorNumbers("TE");
//		setAllCollectorNumbers("SH");
//		setAllCollectorNumbers("EX");
//
//		setAllCollectorNumbers("US");
//		setAllCollectorNumbers("UL");
//		setAllCollectorNumbers("UD");
//
//		setAllCollectorNumbers("MM");
//		setAllCollectorNumbers("NE");
//		setAllCollectorNumbers("PY");
//
//		setAllCollectorNumbers("IN");
//		setAllCollectorNumbers("PS");
//		setAllCollectorNumbers("AP");

//		setAllCollectorNumbers("OD");
//		setAllCollectorNumbers("TO");
//		setAllCollectorNumbers("JU");
//
//		setAllCollectorNumbers("ON");
//		setAllCollectorNumbers("LE");
//		setAllCollectorNumbers("SC");
//
//		setAllCollectorNumbers("MR");
//		setAllCollectorNumbers("DS");
//		setAllCollectorNumbers("FD");
//
//		setAllCollectorNumbers("CHK");
//		setAllCollectorNumbers("BOK");
//		setAllCollectorNumbers("SOK");
//
//		setAllCollectorNumbers("RAV");
//		setAllCollectorNumbers("GP");
//		setAllCollectorNumbers("DIS");
//
//		setAllCollectorNumbers("TSP");
//		setAllCollectorNumbers("TSB");
//		setAllCollectorNumbers("PLC");
//		setAllCollectorNumbers("FUT");

//		setAllCollectorNumbers("LRW");
//		setAllCollectorNumbers("MOR");
//		setAllCollectorNumbers("SHM");
		
	}
	
	private void setAllCollectorNumbers(String edition) {
	try {
		int start = 1;
		start = setCollectorNumbers(edition, Color.W, start);
		start = setCollectorNumbers(edition, Color.U, start);
		start = setCollectorNumbers(edition, Color.B, start);
		start = setCollectorNumbers(edition, Color.R, start);
		start = setCollectorNumbers(edition, Color.G, start);
		start = setCollectorNumbers(edition, Color.Gld, start);
		start = setCollectorNumbers(edition, Color.HWU, start);
		start = setCollectorNumbers(edition, Color.HUB, start);
		start = setCollectorNumbers(edition, Color.HBR, start);
		start = setCollectorNumbers(edition, Color.HRG, start);
		start = setCollectorNumbers(edition, Color.HWG, start);
		start = setCollectorNumbers(edition, Color.HWB, start);
		start = setCollectorNumbers(edition, Color.HWR, start);
		start = setCollectorNumbers(edition, Color.HUR, start);
		start = setCollectorNumbers(edition, Color.HUG, start);
		start = setCollectorNumbers(edition, Color.HBG, start);
		start = setCollectorNumbers(edition, Color.HGLD, start);
		start = setCollectorNumbers(edition, Color.SGLD, start);
		start = setCollectorNumbers(edition, Color.Art, start);
		start = setCollectorNumbersLands(edition, start);

	}
	catch (MtgException e) {
		log.error("Error when applying collector-numbers");
	}
}
	
	public int setCollectorNumbers(String edition, Color color, int firstNumber) throws MtgException {
		Edition ed = em.find(Edition.class, edition);
		if (null == ed) {
			throw new MtgException("Edition not found: " + edition);
		}
		cardList.getCard().setEdition(ed);
		cardList.getCard().setColor(color);
		Object[] cards = cardList.getResultList().toArray();
		Card c = null;
		for (int i = 0; i < cards.length; i++) {
			c = (Card)cards[i];
			c.setCollectorNumber(firstNumber);
			c = em.merge(c);
			em.persist(c);
			log.info("Card: #0 #1 #2 #3", firstNumber++, c.getEdition().getName(), c.getColor(), c.getCardName());
		}
//		em.flush();
		return firstNumber;
	}

	
	
	public int setCollectorNumbersLands(String edition, int firstNumber) throws MtgException {
		Edition ed = em.find(Edition.class, edition);
		if (null == ed) {
			throw new MtgException("Edition not found: " + edition);
		}
		cardList.getCard().setEdition(ed);
		cardList.getCard().setColor(Color.Lnd);
		Object[] cards = cardList.getResultList().toArray();
		List<Card> basicLand = new ArrayList<Card>();
		Card c = null;
		for (int i = 0; i < cards.length; i++) {
			c = (Card)cards[i];
			if (c.getType().contains("Basic Land")) {
				basicLand.add(c);
			}
			else {
				c.setCollectorNumber(firstNumber);
				c = em.merge(c);
				em.persist(c);
				log.info("Card: #0 #1 #2 #3", firstNumber++, c.getEdition().getName(), c.getColor(), c.getCardName());
			}
		}
		firstNumber += basicLand.size();
		return firstNumber;
	}
	
}
